package com.linuxone.iching.server;

import com.linuxone.iching.client.IChingService;
import com.linuxone.iching.shared.Gua;
import com.linuxone.iching.shared.IchingSet;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

/**
 * The server side implementation of the RPC service.
 */
@SuppressWarnings("serial")
public class IChingServiceImpl extends RemoteServiceServlet implements
		IChingService {
	
	private static final IchingSet iching = GUATools.getIchingSet();
	
	@Override
	public Gua getGUA() {
		int GUANumber = GUATools.genGUA();
		Gua myGUA = null;
		
		for (Gua tempGUA : iching.getGuas()){
			if(tempGUA.getId().compareTo(String.valueOf(GUANumber)) == 0) {
				myGUA = tempGUA;
				break;
			}
		}
		return myGUA;
	}
	
	

	@Override
	public Gua getSecGUA(Gua firstGUA, int[] inTouchAOU) {
		Gua myGUA = null;
		String secondGUASymbol = "";
		char[] charFirstGUASymbol = firstGUA.getSymbol().toCharArray();
		
		for(int i=0;i<6;i++){
			if(inTouchAOU[i]==1){ 
				if(Integer.parseInt(""+charFirstGUASymbol[i])==1){
					secondGUASymbol += "0";
				} else {
					secondGUASymbol += "1";
				}
			} else {
				secondGUASymbol += charFirstGUASymbol[i];
			}
		}
		
		for (Gua tempGUA : iching.getGuas()){
			if(tempGUA.getSymbol().compareTo(secondGUASymbol)==0) {
				myGUA = tempGUA;
				break;
			}
		}
		
		return myGUA;
	}


	@Override
	public int[] getTouchAOU() {
		int TouchAOU = GUATools.genTouchAOU();
		int[] myTouchAOU = {0,0,0,0,0,0};	
		myTouchAOU[TouchAOU] = 1;
		return myTouchAOU;
	}
	
	
}
